#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h> /* ioctl */
#include "public.h"


int xy_popen(char * cmd)
{
  char command[64] = {0};
  FILE * fp;

  snprintf(command, sizeof(command), cmd);
  ac_traces(MSG_LEVEL_INFO, "cmd = %s, cmdlen = %d\n", command, strlen(cmd));

  fp = popen(command, "r");
  if (fp == NULL) {
    ac_traces(MSG_LEVEL_ERROR, "popen fail");
    return -1;
  }
  else 
    ac_traces(MSG_LEVEL_INFO, "popen ok");

  pclose(fp);

  return 0;
}

int xy_popen_with_result(char * cmd, char * cmd_result, int result_len)
{
  char command[64] = {0};
  FILE * fp;

  snprintf(command, sizeof(command), cmd);
  ac_traces(MSG_LEVEL_INFO, "cmd = %s, cmdlen = %d\n", command, strlen(cmd));

  fp = popen(command, "r");
  if (fp == NULL) {
    ac_traces(MSG_LEVEL_ERROR, "popen fail");
    return -1;
  }
  else 
    ac_traces(MSG_LEVEL_INFO, "popen ok");

  if(cmd_result != NULL)
    fgets(cmd_result, result_len, fp);

  pclose(fp);

  return 0;
}


